#
# Copyright 2024 Kumo.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###########################################################################
# below are examples, after initialization, modify it to your own.
###########################################################################
###kmcmake_example
#[[
kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME foo
        SOURCES
        foo.cc
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        PLINKS
        ${KMCMAKE_DEPS_LINK}
        PUBLIC
)
]]

add_subdirectory(compiler)

###kmcmake_example


include(configure_checks)
# Create the thrift C++ library
set(thriftcpp_SOURCES
        TApplicationException.cpp
        TOutput.cpp
        async/TAsyncChannel.cpp
        async/TAsyncProtocolProcessor.cpp
        async/TConcurrentClientSyncInfo.h
        async/TConcurrentClientSyncInfo.cpp
        concurrency/ThreadManager.cpp
        concurrency/TimerManager.cpp
        processor/PeekProcessor.cpp
        protocol/TBase64Utils.cpp
        protocol/TDebugProtocol.cpp
        protocol/TJSONProtocol.cpp
        protocol/TMultiplexedProtocol.cpp
        protocol/TProtocol.cpp
        transport/TTransportException.cpp
        transport/TFDTransport.cpp
        transport/TSimpleFileTransport.cpp
        transport/THttpTransport.cpp
        transport/THttpClient.cpp
        transport/THttpServer.cpp
        transport/TSocket.cpp
        transport/TSocketPool.cpp
        transport/TServerSocket.cpp
        transport/TTransportUtils.cpp
        transport/TBufferTransports.cpp
        transport/SocketCommon.cpp
        server/TConnectedClient.cpp
        server/TServerFramework.cpp
        server/TSimpleServer.cpp
        server/TThreadPoolServer.cpp
        server/TThreadedServer.cpp
)

# These files don't work on Windows CE as there is no pipe support
# TODO: These files won't work with UNICODE support on windows. If fixed this can be re-added.
if (NOT WINCE)
    list(APPEND thriftcpp_SOURCES
            transport/TPipe.cpp
            transport/TPipeServer.cpp
            transport/TFileTransport.cpp
    )
endif ()


if (WIN32)
    list(APPEND thriftcpp_SOURCES
            windows/TWinsockSingleton.cpp
            windows/SocketPair.cpp
            windows/GetTimeOfDay.cpp
            windows/WinFcntl.cpp
    )
    if (NOT WINCE)
        # This file uses pipes so it currently won't work on Windows CE
        list(APPEND thriftcpp_SOURCES
                windows/OverlappedSubmissionThread.cpp
        )
    endif ()
else ()
    # These files evaluate to nothing on Windows, so omit them from the
    # Windows build
    list(APPEND thriftcpp_SOURCES
            VirtualProfiling.cpp
            server/TServer.cpp
    )
endif ()


set(thriftcpp_threads_SOURCES
        concurrency/ThreadFactory.cpp
        concurrency/Thread.cpp
        concurrency/Monitor.cpp
        concurrency/Mutex.cpp
)

# Thrift non blocking server
set(thriftcppnb_SOURCES
        server/TNonblockingServer.cpp
        transport/TNonblockingServerSocket.cpp
        async/TEvhttpServer.cpp
        async/TEvhttpClientChannel.cpp
)

list(APPEND thriftcppnb_SOURCES
        transport/TNonblockingSSLServerSocket.cpp
)

# Thrift zlib transport
set(thriftcppz_SOURCES
        transport/TZlibTransport.cpp
        protocol/THeaderProtocol.cpp
        transport/THeaderTransport.cpp
        protocol/THeaderProtocol.cpp
        transport/THeaderTransport.cpp
)

if (WIN32)
    set(thrift_win32 ws2_32)
endif ()

if (MSVC)
    set(msvc_def "-DUNICODE -D_UNICODE")
endif ()

kmcmake_cc_library(
        NAMESPACE ${PROJECT_NAME}
        NAME kmthrift
        SOURCES
        ${thriftcpp_SOURCES}
        ${thriftcpp_threads_SOURCES}
        ${thriftcppnb_SOURCES}
        CXXOPTS
        ${KMCMAKE_CXX_OPTIONS}
        DEFINES
        ${msvc_def}
        PLINKS
        ${KMCMAKE_DEPS_LINK}
        ${thrift_win32}
        PUBLIC
)